<?php

namespace Admin\Controller;

class NotifyController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Notify");
    }

    //首页
    public function index() {
        $userId=$_SESSION['ADMIN_ID'];
        $user=D("admin")->where("user_id=$userId")->find();

        $count = $this->currentModel->where(array("enterprise_code" => $user["enterprise_code"]))->count();

        $page = $this->page($count, 30);
        $list = $this->currentModel
            ->where(array("enterprise_code" => $user["enterprise_code"]))
//            ->order("granted asc")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $allRoleList = D("AdminRole")->select();
        $allEmployeeList = D("Admin")->where(array("enterprise_code" => $user["enterprise_code"]))->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("user", $user);
        $this->assign("users", $list);
        $this->assign("allRoleList", $allRoleList);
        $this->assign("allEmployeeList", $allEmployeeList);
        $this->display();

    }
    public function remove() {

        $id = $_REQUEST['id'];
        $this->currentModel->deleteById($id);

        $this->success('删除成功!', U("Notify/index"));

    }

    public function add() {
        $userId=$_SESSION['ADMIN_ID'];
        $user=D("admin")->where("user_id=$userId")->find();
        $roleArr = D('AdminRole')
            ->where("id < 100")
            ->order("id asc")
            ->select();
        $this->assign("roleArr", $roleArr);
        $allEmployeeList = D("Admin")->where(array("enterprise_code" => $user["enterprise_code"]))->select();
        $this->assign("allEmployeeList", $allEmployeeList);
        $this->display("add");
    }

    /**
     * 添加数据
     */
    public function addPost() {
        $id = $_REQUEST["id"];
        $data = $this->currentModel->queryById($id);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];
        $data["update_time"] = getCurrentDate();
        $data["create_time"] = getCurrentDate();

        $data["notify_name"] = $_REQUEST["notifyName"];
        $data["show_name"] = $_REQUEST["showName"];
        $data["role"] = $_REQUEST["roleId"];
        $data["customer_id"] = $_REQUEST["customerIdStr"];
        $data["note"] = $_REQUEST["note"];
        $this->currentModel->add($data);

        $this->success("添加成功", U("Notify/index"));

    }

    public function edit() {
        $id = $_REQUEST["id"];
        $info = D("Notify")->where("notify_id=$id")->find();
        $this->assign("info", $info);

        $userId=$_SESSION['ADMIN_ID'];
        $user=D("admin")->where("user_id=$userId")->find();

        $roleArr = D('AdminRole')
            ->where("id < 100")
            ->order("id asc")
            ->select();
        $notifyRoleArr = explode(",", $info["role"]);
        $notifyCustomerIdArr = explode(",", $info["customer_id"]);
        $notifyCustomerList = D("Customer")->where(array("customer_id" => array('IN',$notifyCustomerIdArr)))->select();

        $this->assign("roleArr", $roleArr);
        $this->assign("notifyRoleArr", $notifyRoleArr);
        $this->assign("notifyCustomerIdArr", $notifyCustomerIdArr);
        $this->assign("notifyCustomerList", $notifyCustomerList);
        $updater = D("Admin")->where(array("employee_id" => $info["update_id"]))->find();
        $this->assign('updateName', $updater["admin_name"]);

        $allEmployeeList = D("Admin")->where(array("enterprise_code" => $user["enterprise_code"]))->select();
        $this->assign("allEmployeeList", $allEmployeeList);
        $this->display("edit");
    }
    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];
        $data = $this->currentModel->queryById($id);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];
        $data["update_time"] = getCurrentDate();

        $data["notify_name"] = $_REQUEST["notifyName"];
        $data["show_name"] = $_REQUEST["showName"];
        $data["role"] = $_REQUEST["roleId"];
        $data["customer_id"] = $_REQUEST["customerIdStr"];
        $data["note"] = $_REQUEST["note"];
        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("Notify/index"));

    }
}